#include "Direct3DFramework.h"

int Direct3DFramework::updateCamera(DCamera& a_camera)
{
	//temp vars
	static D3DXMATRIX  mPitch;
	static D3DXMATRIX  mRoll;
	static D3DXMATRIX  mYaw;
	D3DXMatrixIdentity(&mPitch);
	D3DXMatrixIdentity(&mRoll);
	D3DXMatrixIdentity(&mYaw);

	//set properties
	m_camAspect = a_camera.m_aspect;
	m_camFov    = a_camera.m_fov;
	m_camNear   = a_camera.m_near;
	m_camFar    = a_camera.m_far;

	//default
	m_camPos    = D3DXVECTOR3(0,0,0);
	m_camLook   = D3DXVECTOR3(0,0,1);
	m_camUp     = D3DXVECTOR3(0,1,0);
	m_camRight  = D3DXVECTOR3(1,0,0);

	//calculate yaw
	D3DXMatrixRotationAxis(&mYaw,&m_camUp,a_camera.m_yaw);
	D3DXVec3TransformCoord(&m_camLook,&m_camLook,&mYaw);
	D3DXVec3TransformCoord(&m_camRight,&m_camRight,&mYaw);

	//calculate pitch
	D3DXMatrixRotationAxis(&mPitch,&m_camRight,a_camera.m_pitch);
	D3DXVec3TransformCoord(&m_camLook,&m_camLook,&mPitch);
	D3DXVec3TransformCoord(&m_camUp,&m_camUp,&mPitch);

	//calculate roll
	D3DXMatrixRotationAxis(&mRoll,&m_camLook,a_camera.m_roll);
	D3DXVec3TransformCoord(&m_camRight,&m_camRight,&mRoll);
	D3DXVec3TransformCoord(&m_camUp,&m_camUp,&mRoll);

	//return values
	a_camera.m_look.set(m_camLook.x,m_camLook.y,m_camLook.z);
	a_camera.m_up.set(m_camUp.x,m_camUp.y,m_camUp.z);
	a_camera.m_right.set(m_camRight.x,m_camRight.y,m_camRight.z);
	a_camera.m_look.normalize();
	a_camera.m_up.normalize();
	a_camera.m_right.normalize();

	//add distance
	if(a_camera.m_dis)
		m_camPos += m_camLook * a_camera.m_dis;

	//apply positioning
	m_camPos += D3DXVECTOR3(
		a_camera.m_pos.x,
		a_camera.m_pos.y,
		a_camera.m_pos.z);
	m_camLook += D3DXVECTOR3(
		a_camera.m_pos.x,
		a_camera.m_pos.y,
		a_camera.m_pos.z);

	//setup veiw matrix
	D3DXMatrixLookAtLH(
		&m_view,
		&m_camPos,
		&m_camLook,
		&m_camUp);

	//setup projection matrix
	D3DXMatrixPerspectiveFovLH(
		&m_proj,
		m_camFov,
		m_camAspect,
		m_camNear,
		m_camFar);

	return D3D_SUCCESS;
}

int Direct3DFramework::updateTransform(
	DMesh* a_mesh,
	V3DF a_cen,
	V3DF a_trn,
	V3DF a_scl,
	V3DF a_rot,
	V3DF* a_look,
	V3DF* a_right)
{
	static D3DXMATRIX cen;
	static D3DXMATRIX trn;
	static D3DXMATRIX scl;
	static D3DXMATRIX rotX;
	static D3DXMATRIX rotY;
	static D3DXMATRIX rotZ;

	D3DXMatrixIdentity(&cen);
	D3DXMatrixIdentity(&trn);
	D3DXMatrixIdentity(&scl);
	D3DXMatrixIdentity(&rotX);
	D3DXMatrixIdentity(&rotY);
	D3DXMatrixIdentity(&rotZ);

	D3DXMatrixTranslation(&cen,a_cen.x,a_cen.y,a_cen.z);
	D3DXMatrixTranslation(&trn,a_trn.x,a_trn.y,a_trn.z);
	D3DXMatrixScaling(&scl,a_scl.x,a_scl.y,a_scl.z);
	D3DXMatrixRotationX(&rotX,D3DXToRadian(a_rot.x));
	D3DXMatrixRotationY(&rotY,D3DXToRadian(a_rot.y));
	D3DXMatrixRotationZ(&rotZ,D3DXToRadian(a_rot.z));

	if(a_look)
	{
		D3DXVECTOR3 look(a_look->x,a_look->y,a_look->z);
		D3DXVec3TransformCoord(&look,&look,&rotY);
		a_look->set(look.x,look.y,look.z);
		a_look->normalize();
	}
	
	if(a_right)
	{
		D3DXVECTOR3 right(a_right->x,a_right->y,a_right->z);
		D3DXVec3TransformCoord(&right,&right,&rotY);
		a_right->set(right.x,right.y,right.z);
		a_right->normalize();
	}

	D3DFMesh* mesh = m_meshList[a_mesh->m_meshId];
	mesh->m_world[a_mesh->m_meshMat] = cen*scl*rotZ*rotY*rotX*trn;

	return D3D_SUCCESS;
}

int Direct3DFramework::updateTransform(
	DTexture2D* a_texture,
	V2DF  a_cen,
	V2DF  a_trn,
	V2DF  a_scl,
	float a_rot)
{
	D3DFTexture2D* texture = m_texture2DList[a_texture->m_texId];
	texture->m_world[a_texture->m_texMat].m_cen = D3DXVECTOR2(a_cen.x,a_cen.y);
	texture->m_world[a_texture->m_texMat].m_trn = D3DXVECTOR2(a_trn.x,a_trn.y);
	texture->m_world[a_texture->m_texMat].m_scl = D3DXVECTOR2(a_scl.x,a_scl.y);
	texture->m_world[a_texture->m_texMat].m_rot = a_rot;

	return D3D_SUCCESS;
}